/*
 * FIFO IO manipulations header file
 *
 * Copyright (C) 2020 Long-Horn.
 *
 */
#ifndef FIFO_IO_H
#define FIFO_IO_H

#include <stdio.h>
#include <pthread.h>

#ifdef __cplusplus
extern "C"  {
#endif

#define FIFO_NAME_LEN 32

typedef struct _fifo_io
{
    // interface functions
    int (* open)(void *fio);
    void (* close)(void *fio);
    int (* read)(void *fio, void *buffer, size_t count);
    int (* write)(void *fio, void *buffer, size_t count);
    int (* write_timeout)(void *fio, void *buffer, size_t count, size_t ms);
    int (* read_timeout)(void *fio, void *buffer, size_t count, size_t ms);
    int (* write_with_response)(void *fio, void *buffer, size_t count, void *response);
    
    // name and file hande for write and read FIFOS
    int wfd;
    int rfd;
    char wr_fifoname[FIFO_NAME_LEN];
    char rd_fifoname[FIFO_NAME_LEN];
    
    // synchronization primitives
    pthread_mutex_t write_mutex;
    pthread_mutex_t read_mutex;
} fifo_io_s;

int create_fifo_io(fifo_io_s **fio, const char *rd_fifoname, const char *wr_fifoname);
void destroy_fifo_io(fifo_io_s *fio);

#ifdef __cplusplus
}
#endif

#endif	/* FIFO_IO_H */
